package com.zhida.jiyangcenter.address

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.lifecycle.lifecycleScope
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.*
import chooongg.http.APIException
import chooongg.http.HttpCallback
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.AddressDetailsEntity
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.NetResponse
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.SP
import com.zhida.jiyangcenter.utils.request
import com.zhida.jiyangcenter.widget.AddressPickerView
import kotlinx.android.synthetic.main.activity_add_address.*

@TitleBar(false)
@ContentLayout(R.layout.activity_add_address)
class AddAddressActivity : BaseActivity() {
    var provinceCode = ""
    var cityCode = ""
    var districtCode = ""
    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }.title(if (intent.getStringExtra("uuid").isNullOrEmpty()) "添加地址" else "修改地址")
        ll_check_box.setOnClickListener {
            check_box.setChecked(!check_box.isChecked, true)
        }
        et_address_province.setOnClickListener {
            val pickerView = AddressPickerView(context)
            val popupWindow =
                PopupWindow(pickerView, ViewGroup.LayoutParams.MATCH_PARENT, screenHeight / 2)
            pickerView.setOnAddressPickerSure { address, provinceCode, cityCode, districtCode ->
                if (!address.isNullOrEmpty()) {
                    this.provinceCode = provinceCode
                    this.cityCode = cityCode
                    this.districtCode = districtCode
                    et_address_province.text = address
                    et_address_province.setTextColor(resColor(R.color.textVital))
                }
                popupWindow.dismiss()
            }
            popupWindow.isFocusable = true
            popupWindow.setBackgroundDrawable(ColorDrawable(Color.parseColor("#FFFFFF")))
            popupWindow.isOutsideTouchable = true
            popupWindow.isTouchable = true
            popupWindow.animationStyle = R.style.mypopwindow_anim_style
            popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0)
        }
        tv_address_commit.setOnClickListener {
            when {
                et_address_name.text.toString().isEmpty() -> {
                    toast("请填写您的收货姓名")
                }
                et_address_phone.text.toString().isEmpty() -> {
                    toast("请填写您的手机号码")
                }
                provinceCode.isEmpty() || cityCode.isEmpty() || districtCode.isEmpty() -> {
                    toast("请选择您的所在地区")
                }
                et_address_details.text.toString().isEmpty() -> {
                    toast("请填写您的详细地址")
                }

                else -> {
                    if (intent.getStringExtra("uuid").isNullOrEmpty())
                        lifecycleScope.request({
                            APIAccount.service().addressAdd(
                                et_address_name.text.toString(),
                                et_address_phone.text.toString(),
                                et_address_youzheng.text.toString(),
                                et_address_details.text.toString(),
                                if (check_box.isChecked) "1" else "0",
                                provinceCode,
                                cityCode,
                                districtCode
                            )
                        }, object : BaseAPI<Any>() {

                            override fun onError(e: APIException) {
                                tipFail(e.eMessage)
                            }

                            override fun onSuccess(t: Any?, state: Int, msg: String) {
                                tipSuccess(msg)
                                finish()
                            }

                        })
                    else
                        lifecycleScope.request({
                            APIAccount.service().addressCompile2(
                                et_address_name.text.toString(),
                                et_address_phone.text.toString(),
                                et_address_youzheng.text.toString(),
                                et_address_details.text.toString(),
                                if (check_box.isChecked) "1" else "0",
                                provinceCode,
                                cityCode,
                                districtCode,
                                intent.getStringExtra("uuid")
                            )
                        }, object : BaseAPI<Any>() {
                            override fun onSuccess(t: Any?, state: Int, msg: String) {
                                tipSuccess(msg)
                                finish()
                            }

                            override fun onError(e: APIException) {
                                tipFail(e.eMessage)
                            }
                        })
                }
            }
        }
        if (!intent.getStringExtra("uuid").isNullOrEmpty())
            getData()
    }

    private fun getData() {
        lifecycleScope.request({
            APIAccount.service().addressCompile1(
                intent.getStringExtra("uuid")
            )
        }, object : BaseAPI<AddressDetailsEntity>() {

            override fun onError(e: APIException) {
                tipFail(e.eMessage)
            }

            override fun onSuccess(t: AddressDetailsEntity?, state: Int, msg: String) {
                et_address_name.setText(t?.name)
                et_address_phone.setText(t?.phone)
                et_address_youzheng.setText(t?.postcode)
                et_address_province.text = t?.area
                provinceCode = t?.area?.split("-")?.get(0) ?: ""
                cityCode = t?.area?.split("-")?.get(1) ?: ""
                districtCode = t?.area?.split("-")?.get(2) ?: ""

                et_address_details.setText(t?.detail)
                check_box.setChecked(t?.is_default ?: false, true)
            }

        })
    }
}